<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
    <title>Coverage Report :: CollisionInteractionMap</title>
    <style type="text/css">
        @import "../../.css/coverage.css";
    </style>
</head>

<body>
<div class="header"></div>

<div class="content">
    <div class="breadCrumbs">
        [ <a href="../../index.html">all classes</a> ]
        [ <a href="../index.html">nl.tudelft.jpacman.level</a> ]
    </div>

    <h1>Coverage Summary for Class: CollisionInteractionMap (nl.tudelft.jpacman.level)</h1>

    <table class="coverageStats">

        <tr>
            <th class="name">Class</th>
            <th class="coverageStat
">
                Method, %
            </th>
            <th class="coverageStat
">
                Line, %
            </th>
        </tr>
        <tr>
            <td class="name">CollisionInteractionMap</td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 7)
  </span>
            </td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 38)
  </span>
            </td>
        </tr>
        <tr>
            <td class="name">CollisionInteractionMap$InverseCollisionHandler</td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 2)
  </span>
            </td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 3)
  </span>
            </td>
        </tr>
        <tr>
            <td class="name"><strong>total</strong></td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 9)
  </span>
            </td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 41)
  </span>
            </td>
        </tr>
    </table>

    <br/>
    <br/>


    <div class="sourceCode"><i>1</i>&nbsp;package nl.tudelft.jpacman.level;
        <i>2</i>&nbsp;
        <i>3</i>&nbsp;import java.util.ArrayList;
        <i>4</i>&nbsp;import java.util.HashMap;
        <i>5</i>&nbsp;import java.util.List;
        <i>6</i>&nbsp;import java.util.Map;
        <i>7</i>&nbsp;
        <i>8</i>&nbsp;import nl.tudelft.jpacman.board.Unit;
        <i>9</i>&nbsp;
        <i>10</i>&nbsp;/**
        <i>11</i>&nbsp; * A map of possible collisions and their handlers.
        <i>12</i>&nbsp; *
        <i>13</i>&nbsp; * @author Michael de Jong
        <i>14</i>&nbsp; * @author Jeroen Roosen
        <i>15</i>&nbsp; */
        <i>16</i>&nbsp;public class CollisionInteractionMap implements CollisionMap {
        <i>17</i>&nbsp;
        <i>18</i>&nbsp; /**
        <i>19</i>&nbsp; * The collection of collision handlers.
        <i>20</i>&nbsp; */
        <i>21</i>&nbsp; private final Map&lt;Class&lt;? extends Unit&gt;,
        <i>22</i>&nbsp; Map&lt;Class&lt;? extends Unit&gt;, CollisionHandler&lt;?, ?&gt;&gt;&gt; handlers;
        <i>23</i>&nbsp;
        <i>24</i>&nbsp; /**
        <i>25</i>&nbsp; * Creates a new, empty collision map.
        <i>26</i>&nbsp; */
        <b class="nc"><i>27</i>&nbsp; public CollisionInteractionMap() {</b>
        <b class="nc"><i>28</i>&nbsp; this.handlers = new HashMap&lt;&gt;();</b>
        <i>29</i>&nbsp; }
        <i>30</i>&nbsp;
        <i>31</i>&nbsp; /**
        <i>32</i>&nbsp; * Adds a two-way collision interaction to this collection, i.e. the
        <i>33</i>&nbsp; * collision handler will be used for both C1 versus C2 and C2 versus C1.
        <i>34</i>&nbsp; *
        <i>35</i>&nbsp; * @param &lt;C1&gt;
        <i>36</i>&nbsp; * The collider type.
        <i>37</i>&nbsp; * @param &lt;C2&gt;
        <i>38</i>&nbsp; * The collidee (unit that was moved into) type.
        <i>39</i>&nbsp; *
        <i>40</i>&nbsp; * @param collider
        <i>41</i>&nbsp; * The collider type.
        <i>42</i>&nbsp; * @param collidee
        <i>43</i>&nbsp; * The collidee type.
        <i>44</i>&nbsp; * @param handler
        <i>45</i>&nbsp; * The handler that handles the collision.
        <i>46</i>&nbsp; */
        <i>47</i>&nbsp; public &lt;C1 extends Unit, C2 extends Unit&gt; void onCollision(
        <i>48</i>&nbsp; Class&lt;C1&gt; collider, Class&lt;C2&gt; collidee, CollisionHandler&lt;C1, C2&gt; handler) {
        <b class="nc"><i>49</i>&nbsp; onCollision(collider, collidee, true, handler);</b>
        <i>50</i>&nbsp; }
        <i>51</i>&nbsp;
        <i>52</i>&nbsp; /**
        <i>53</i>&nbsp; * Adds a collision interaction to this collection.
        <i>54</i>&nbsp; *
        <i>55</i>&nbsp; * @param &lt;C1&gt;
        <i>56</i>&nbsp; * The collider type.
        <i>57</i>&nbsp; * @param &lt;C2&gt;
        <i>58</i>&nbsp; * The collidee (unit that was moved into) type.
        <i>59</i>&nbsp; *
        <i>60</i>&nbsp; * @param collider
        <i>61</i>&nbsp; * The collider type.
        <i>62</i>&nbsp; * @param collidee
        <i>63</i>&nbsp; * The collidee type.
        <i>64</i>&nbsp; * @param symetric
        <i>65</i>&nbsp; * &lt;code&gt;true&lt;/code&gt; if this collision is used for both
        <i>66</i>&nbsp; * C1 against C2 and vice versa;
        <i>67</i>&nbsp; * &lt;code&gt;false&lt;/code&gt; if only for C1 against C2.
        <i>68</i>&nbsp; * @param handler
        <i>69</i>&nbsp; * The handler that handles the collision.
        <i>70</i>&nbsp; */
        <i>71</i>&nbsp; public &lt;C1 extends Unit, C2 extends Unit&gt; void onCollision(
        <i>72</i>&nbsp; Class&lt;C1&gt; collider, Class&lt;C2&gt; collidee, boolean symetric,
        <i>73</i>&nbsp; CollisionHandler&lt;C1, C2&gt; handler) {
        <b class="nc"><i>74</i>&nbsp; addHandler(collider, collidee, handler);</b>
        <b class="nc"><i>75</i>&nbsp; if (symetric) {</b>
        <b class="nc"><i>76</i>&nbsp; addHandler(collidee, collider, new InverseCollisionHandler&lt;&gt;(handler));</b>
        <i>77</i>&nbsp; }
        <i>78</i>&nbsp; }
        <i>79</i>&nbsp;
        <i>80</i>&nbsp; /**
        <i>81</i>&nbsp; * Adds the collision interaction..
        <i>82</i>&nbsp; *
        <i>83</i>&nbsp; * @param collider
        <i>84</i>&nbsp; * The collider type.
        <i>85</i>&nbsp; * @param collidee
        <i>86</i>&nbsp; * The collidee type.
        <i>87</i>&nbsp; * @param handler
        <i>88</i>&nbsp; * The handler that handles the collision.
        <i>89</i>&nbsp; */
        <i>90</i>&nbsp; private void addHandler(Class&lt;? extends Unit&gt; collider,
        <i>91</i>&nbsp; Class&lt;? extends Unit&gt; collidee, CollisionHandler&lt;?, ?&gt; handler) {
        <b class="nc"><i>92</i>&nbsp; if (!handlers.containsKey(collider)) {</b>
        <b class="nc"><i>93</i>&nbsp; handlers.put(collider, new HashMap&lt;&gt;());</b>
        <i>94</i>&nbsp; }
        <i>95</i>&nbsp;
        <b class="nc"><i>96</i>&nbsp; Map&lt;Class&lt;? extends Unit&gt;, CollisionHandler&lt;?, ?&gt;&gt; map =
            handlers.get(collider);</b>
        <b class="nc"><i>97</i>&nbsp; map.put(collidee, handler);</b>
        <i>98</i>&nbsp; }
        <i>99</i>&nbsp;
        <i>100</i>&nbsp; /**
        <i>101</i>&nbsp; * Handles the collision between two colliding parties, if a suitable
        <i>102</i>&nbsp; * collision handler is listed.
        <i>103</i>&nbsp; *
        <i>104</i>&nbsp; * @param &lt;C1&gt;
        <i>105</i>&nbsp; * The collider type.
        <i>106</i>&nbsp; * @param &lt;C2&gt;
        <i>107</i>&nbsp; * The collidee (unit that was moved into) type.
        <i>108</i>&nbsp; *
        <i>109</i>&nbsp; * @param collider
        <i>110</i>&nbsp; * The collider.
        <i>111</i>&nbsp; * @param collidee
        <i>112</i>&nbsp; * The collidee.
        <i>113</i>&nbsp; */
        <i>114</i>&nbsp; @SuppressWarnings(&quot;unchecked&quot;)
        <i>115</i>&nbsp; @Override
        <i>116</i>&nbsp; public &lt;C1 extends Unit, C2 extends Unit&gt; void collide(C1 collider,
        <i>117</i>&nbsp; C2 collidee) {
        <b class="nc"><i>118</i>&nbsp; Class&lt;? extends Unit&gt; colliderKey = getMostSpecificClass(handlers,
            collider.getClass());</b>
        <b class="nc"><i>119</i>&nbsp; if (colliderKey == null) {</b>
        <i>120</i>&nbsp; return;
        <i>121</i>&nbsp; }
        <i>122</i>&nbsp;
        <b class="nc"><i>123</i>&nbsp; Map&lt;Class&lt;? extends Unit&gt;, CollisionHandler&lt;?, ?&gt;&gt; map =
            handlers.get(colliderKey);</b>
        <b class="nc"><i>124</i>&nbsp; Class&lt;? extends Unit&gt; collideeKey = getMostSpecificClass(map,
            collidee.getClass());</b>
        <b class="nc"><i>125</i>&nbsp; if (collideeKey == null) {</b>
        <i>126</i>&nbsp; return;
        <i>127</i>&nbsp; }
        <i>128</i>&nbsp;
        <b class="nc"><i>129</i>&nbsp; CollisionHandler&lt;C1, C2&gt; collisionHandler = (CollisionHandler&lt;C1, C2&gt;)
            map.get(collideeKey);</b>
        <b class="nc"><i>130</i>&nbsp; if (collisionHandler == null) {</b>
        <i>131</i>&nbsp; return;
        <i>132</i>&nbsp; }
        <i>133</i>&nbsp;
        <b class="nc"><i>134</i>&nbsp; collisionHandler.handleCollision(collider, collidee);</b>
        <i>135</i>&nbsp; }
        <i>136</i>&nbsp;
        <i>137</i>&nbsp; /**
        <i>138</i>&nbsp; * Figures out the most specific class that is listed in the map. I.e. if A
        <i>139</i>&nbsp; * extends B and B is listed while requesting A, then B will be returned.
        <i>140</i>&nbsp; *
        <i>141</i>&nbsp; * @param map
        <i>142</i>&nbsp; * The map with the key collection to find a matching class in.
        <i>143</i>&nbsp; * @param key
        <i>144</i>&nbsp; * The class to search the most suitable key for.
        <i>145</i>&nbsp; * @return The most specific class from the key collection.
        <i>146</i>&nbsp; */
        <i>147</i>&nbsp; private Class&lt;? extends Unit&gt; getMostSpecificClass(
        <i>148</i>&nbsp; Map&lt;Class&lt;? extends Unit&gt;, ?&gt; map, Class&lt;? extends Unit&gt; key) {
        <b class="nc"><i>149</i>&nbsp; List&lt;Class&lt;? extends Unit&gt;&gt; collideeInheritance =
            getInheritance(key);</b>
        <b class="nc"><i>150</i>&nbsp; for (Class&lt;? extends Unit&gt; pointer : collideeInheritance) {</b>
        <b class="nc"><i>151</i>&nbsp; if (map.containsKey(pointer)) {</b>
        <b class="nc"><i>152</i>&nbsp; return pointer;</b>
        <i>153</i>&nbsp; }
        <b class="nc"><i>154</i>&nbsp; }</b>
        <b class="nc"><i>155</i>&nbsp; return null;</b>
        <i>156</i>&nbsp; }
        <i>157</i>&nbsp;
        <i>158</i>&nbsp; /**
        <i>159</i>&nbsp; * Returns a list of all classes and interfaces the class inherits.
        <i>160</i>&nbsp; *
        <i>161</i>&nbsp; * @param clazz
        <i>162</i>&nbsp; * The class to create a list of super classes and interfaces
        <i>163</i>&nbsp; * for.
        <i>164</i>&nbsp; * @return A list of all classes and interfaces the class inherits.
        <i>165</i>&nbsp; */
        <i>166</i>&nbsp; @SuppressWarnings(&quot;unchecked&quot;)
        <i>167</i>&nbsp; private List&lt;Class&lt;? extends Unit&gt;&gt; getInheritance(
        <i>168</i>&nbsp; Class&lt;? extends Unit&gt; clazz) {
        <b class="nc"><i>169</i>&nbsp; List&lt;Class&lt;? extends Unit&gt;&gt; found = new ArrayList&lt;&gt;();</b>
        <b class="nc"><i>170</i>&nbsp; found.add(clazz);</b>
        <i>171</i>&nbsp;
        <b class="nc"><i>172</i>&nbsp; int index = 0;</b>
        <b class="nc"><i>173</i>&nbsp; while (found.size() &gt; index) {</b>
        <b class="nc"><i>174</i>&nbsp; Class&lt;?&gt; current = found.get(index);</b>
        <b class="nc"><i>175</i>&nbsp; Class&lt;?&gt; superClass = current.getSuperclass();</b>
        <b class="nc"><i>176</i>&nbsp; if (superClass != null &amp;&amp; Unit.class.isAssignableFrom(superClass)) {</b>
        <b class="nc"><i>177</i>&nbsp; found.add((Class&lt;? extends Unit&gt;) superClass);</b>
        <i>178</i>&nbsp; }
        <b class="nc"><i>179</i>&nbsp; for (Class&lt;?&gt; classInterface : current.getInterfaces()) {</b>
        <b class="nc"><i>180</i>&nbsp; if (Unit.class.isAssignableFrom(classInterface)) {</b>
        <b class="nc"><i>181</i>&nbsp; found.add((Class&lt;? extends Unit&gt;) classInterface);</b>
        <i>182</i>&nbsp; }
        <i>183</i>&nbsp; }
        <b class="nc"><i>184</i>&nbsp; index++;</b>
        <b class="nc"><i>185</i>&nbsp; }</b>
        <i>186</i>&nbsp;
        <b class="nc"><i>187</i>&nbsp; return found;</b>
        <i>188</i>&nbsp; }
        <i>189</i>&nbsp;
        <i>190</i>&nbsp; /**
        <i>191</i>&nbsp; * Handles the collision between two colliding parties.
        <i>192</i>&nbsp; *
        <i>193</i>&nbsp; * @author Michael de Jong
        <i>194</i>&nbsp; *
        <i>195</i>&nbsp; * @param &lt;C1&gt;
        <i>196</i>&nbsp; * The collider type.
        <i>197</i>&nbsp; * @param &lt;C2&gt;
        <i>198</i>&nbsp; * The collidee type.
        <i>199</i>&nbsp; */
        <i>200</i>&nbsp; public interface CollisionHandler&lt;C1 extends Unit, C2 extends Unit&gt; {
        <i>201</i>&nbsp;
        <i>202</i>&nbsp; /**
        <i>203</i>&nbsp; * Handles the collision between two colliding parties.
        <i>204</i>&nbsp; *
        <i>205</i>&nbsp; * @param collider
        <i>206</i>&nbsp; * The collider.
        <i>207</i>&nbsp; * @param collidee
        <i>208</i>&nbsp; * The collidee.
        <i>209</i>&nbsp; */
        <i>210</i>&nbsp; void handleCollision(C1 collider, C2 collidee);
        <i>211</i>&nbsp; }
        <i>212</i>&nbsp;
        <i>213</i>&nbsp; /**
        <i>214</i>&nbsp; * An symmetrical copy of a collision hander.
        <i>215</i>&nbsp; *
        <i>216</i>&nbsp; * @author Michael de Jong
        <i>217</i>&nbsp; *
        <i>218</i>&nbsp; * @param &lt;C1&gt;
        <i>219</i>&nbsp; * The collider type.
        <i>220</i>&nbsp; * @param &lt;C2&gt;
        <i>221</i>&nbsp; * The collidee type.
        <i>222</i>&nbsp; */
        <i>223</i>&nbsp; private static class InverseCollisionHandler&lt;C1 extends Unit, C2 extends Unit&gt;
        <i>224</i>&nbsp; implements CollisionHandler&lt;C1, C2&gt; {
        <i>225</i>&nbsp;
        <i>226</i>&nbsp; /**
        <i>227</i>&nbsp; * The handler of this collision.
        <i>228</i>&nbsp; */
        <i>229</i>&nbsp; private final CollisionHandler&lt;C2, C1&gt; handler;
        <i>230</i>&nbsp;
        <i>231</i>&nbsp; /**
        <i>232</i>&nbsp; * Creates a new collision handler.
        <i>233</i>&nbsp; *
        <i>234</i>&nbsp; * @param handler
        <i>235</i>&nbsp; * The symmetric handler for this collision.
        <i>236</i>&nbsp; */
        <b class="nc"><i>237</i>&nbsp; InverseCollisionHandler(CollisionHandler&lt;C2, C1&gt; handler) {</b>
        <b class="nc"><i>238</i>&nbsp; this.handler = handler;</b>
        <i>239</i>&nbsp; }
        <i>240</i>&nbsp;
        <i>241</i>&nbsp; /**
        <i>242</i>&nbsp; * Handles this collision by flipping the collider and collidee, making
        <i>243</i>&nbsp; * it compatible with the initial collision.
        <i>244</i>&nbsp; */
        <i>245</i>&nbsp; @Override
        <i>246</i>&nbsp; public void handleCollision(C1 collider, C2 collidee) {
        <b class="nc"><i>247</i>&nbsp; handler.handleCollision(collidee, collider);</b>
        <i>248</i>&nbsp; }
        <i>249</i>&nbsp; }
        <i>250</i>&nbsp;
        <i>251</i>&nbsp;}
    </div>
</div>

<div class="footer">

    <div style="float:right;">generated on 2023-04-25 16:45</div>
</div>
</body>
</html>
